<?php /** @noinspection ALL */

namespace app\admin\controller;

use Exception;
use think\response\Json;

class Admin extends Base
{

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 获取当前登录的用户信息
     */
    public function get_login_user(): Json
    {
        $userId = request()->user['id'];
        $admin = $this->getAdminService()->getAdminById($userId);
        $data = [
            'id' => $admin->getId(),
            'username' => $admin->getUsername(),
            'status' => $admin->getStatus(),
            'statusRemark' => $admin->getStatusRemark(),
            'isSuper' => $admin->getIsSuper(),
            'addTime' => $admin->getAddTime(),
        ];
        return $this->requestSuccessReturn($data);
    }

    /**
     * 添加管理员
     */
    public function add_admin(): Json
    {
        try {
            //用户名
            $username = request()->post('username');
            //密码
            $password = request()->post('password');
            if (empty($username)) {
                throw new Exception("用户名必传");
            }
            if (empty($password)) {
                throw new Exception("密码必传");
            }

            $admin = new \service\vo\admin\Admin();
            $admin->setUsername($username);
            $admin->setPassword($password);
            $id = $this->getAdminService()->addAdmin($admin);
            $data = ['id' => $id];
            return $this->requestSuccessReturn($data);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }

    //编辑管理员
    public function edit_admin(): Json
    {
        try {
            //id
            $id = request()->put('id');
            //密码
            $password = request()->put('password');
            //状态
            $status = request()->put('status');
            //状态描述
            $statusRemark = request()->put('statusRemark');
            if (empty($id)) {
                throw new Exception("id必传");
            }

            $admin = new \service\vo\admin\Admin();
            $admin->setId($id);
            $admin->setPassword("");
            if (!empty($password)) {
                $admin->setPassword($password);
            }
            $admin->setStatus($status);
            $admin->setStatusRemark($statusRemark);
            $this->getAdminService()->editAdmin($admin);
            return $this->requestSuccessReturn([]);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }

    //获取管理员
    public function get_admin(): Json
    {
        try {
            //id
            $id = request()->get('id');

            if (empty($id)) {
                throw new Exception("id必传");
            }

            $admin = $this->getAdminService()->getAdminById($id);
            $data = [
                'id' => $admin->getId(),
                'username' => $admin->getUsername(),
                'status' => $admin->getStatus(),
                'statusRemark' => $admin->getStatusRemark(),
                'addTime' => $admin->getAddTime(),
            ];
            return $this->requestSuccessReturn($data);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }

    //根据ids获取管理员列表
    public function get_admins(): Json
    {
        try {
            //id
            $ids = request()->get('ids');

            if (empty($ids)) {
                throw new Exception("ids必传");
            }
            $idsArr = explode(',', $ids);
            if (count($idsArr) == 0) {
                throw new Exception("ids必传");
            }
            $adminList = $this->getAdminService()->getAdminsByIds($idsArr);
            $data = [];
            foreach ($adminList as $admin) {
                $data[] = [
                    'id' => $admin->getId(),
                    'username' => $admin->getUsername(),
                    'status' => $admin->getStatus(),
                    'statusRemark' => $admin->getStatusRemark(),
                    'addTime' => $admin->getAddTime()
                ];
            }
            return $this->requestSuccessReturn($data);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }

    //删除管理员
    public function delete_admin(): Json
    {
        try {
            //id
            $id = request()->delete('id');

            if (empty($id)) {
                throw new Exception("id必传");
            }

            $result = $this->getAdminService()->deleteAdmin($id);
            if ($result == 0) {
                throw new Exception("删除失败");
            }
            return $this->requestSuccessReturn([]);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }

    //搜索管理员
    public function search_admin(): Json
    {
        try {
            //接收传递过来的条件参数
            //用户名
            $username = request()->get('username') ?? "";
            //状态
            $status = request()->get('status') ?? "";
            //开始时间
            $startTime = request()->get('startTime') ?? "";
            //结束时间
            $endTime = request()->get('endTime') ?? "";

            //过滤条件
            $filterCriteria = [];
            if (!empty($username)) {
                $filterCriteria['Name'] = $username;
            }
            if ($status != "") {
                $filterCriteria['Status'] = $status;
            }
            if (!empty($startTime) || !empty($endTime)) {
                $filterCriteria['AddTime'] = ['start' => $startTime, 'end' => $endTime];
            }

            //排序条件
            $sorterCriteria = ['ID' => false];

            $dataList = $this->getAdminService()->searchAdmin($this->page, $this->pageSize, $filterCriteria, $sorterCriteria);
            $dataArr = $dataList->getData();
            $dataCount = $dataList->getCount();

            $data = [];
            if (!empty($dataArr)) {
                foreach ($dataArr as $val) {
                    $data[] = [
                        'id' => $val->getId(),
                        'username' => $val->getUsername(),
                        'status' => $val->getStatus(),
                        'statusRemark' => $val->getStatusRemark(),
                        'addTime' => $val->getAddTime()
                    ];
                }
            }
            $resultData = [
                "total" => $dataCount,
                "records" => $data
            ];
            return $this->requestSuccessReturn($resultData);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }
}